package org.itsolutions.javaBackup.gui.web.common.composites;

import java.util.concurrent.atomic.AtomicBoolean;

import org.itsolutions.javaBackup.gui.web.common.IAutenticatedApplication;
import org.itsolutions.javaBackup.gui.web.common.components.IView;
import org.itsolutions.javaBackup.gui.web.common.components.JBButton;
import org.itsolutions.javaBackup.gui.web.common.components.JBFormLayout;
import org.itsolutions.javaBackup.gui.web.common.components.JBGridLayout;
import org.itsolutions.javaBackup.gui.web.common.components.JBLabel;
import org.itsolutions.javaBackup.gui.web.common.components.JBPanel;
import org.itsolutions.javaBackup.gui.web.common.components.JBPasswordField;
import org.itsolutions.javaBackup.gui.web.common.components.JBTextField;
import org.itsolutions.javaBackup.gui.web.common.components.JBVerticalLayout;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;

import com.google.inject.Inject;
import com.vaadin.Application;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;

/**
 * 
 * @author Tomas Fecko
 */
public final class LoginComposite extends CustomComponent implements IView<LoginComposite> {

	private static final long 		serialVersionUID = -7404174431087532724L;
	private static final ILogger	LOGGER = LoggerHelper.getLogger(LoginComposite.class);
	@Inject JBGridLayout			mGrid;
	@Inject JBLabel					mLabel;
	@Inject JBPanel 				mMainPanel;
	@Inject JBButton 				mLoginButton;
	@Inject JBTextField 			mLogin;
	@Inject JBPasswordField			mPassword;
	@Inject JBFormLayout 			mFormLayout;
	@Inject JBVerticalLayout		mVerticalLayout;
	private final AtomicBoolean		mWasInitCalled = new AtomicBoolean(false);

	@Override
	public ComponentContainer getMainContainer() {
		return this;
	}
	
	@Override
	public LoginComposite init() {
		mLogin.setValue("");
		mPassword.setValue("");
		if (mWasInitCalled.get()) {
			return this;
		}
		setCompositionRoot(mGrid);
		mMainPanel.setCaption("LOGIN.LBL");
		mMainPanel.setWidth(SIZE_UNDEFINED, 0);
		mGrid.setGrid(1, 1);
		mGrid.setWidth("100%");
		mGrid.setHeight("700px");
		
		mFormLayout.setSizeUndefined();
		mLoginButton.setCaption("LOGIN.BTN");
		mLogin.setCaption("USERNAME.TXF");
		mPassword.setCaption("PASSWORD.TXF");
		
		mFormLayout.setMargin(true);
		mFormLayout.setSpacing(true);
		
		mFormLayout.addComponent(mLabel);
		mFormLayout.addComponent(mLogin);
		mFormLayout.addComponent(mPassword);
		mFormLayout.addComponent(mLoginButton);
		
//		mVerticalLayout.setSpacing(true);
		mVerticalLayout.setMargin(true);
		mVerticalLayout.addComponent(mLabel);
		mVerticalLayout.setComponentAlignment(mLabel, Alignment.MIDDLE_CENTER);
		mVerticalLayout.addComponent(mFormLayout);
		mMainPanel.setContent(mVerticalLayout);
		mGrid.addComponent(mMainPanel, 0, 0);
		mGrid.setRowExpandRatio(0, 1);
		mGrid.setComponentAlignment(mMainPanel, Alignment.MIDDLE_CENTER);
		
		mLoginButton.addListener(new Button.ClickListener() {
			
			private static final long serialVersionUID = 7019194950287483570L;
			
			@Override
			public void buttonClick(ClickEvent pEvent) {
	                Application application = getApplication();
	                if (!(application instanceof IAutenticatedApplication)) {
	                	LOGGER.log(ELogLevel.ERROR, "Wrong application type. Application must implement IAutenticatedApplication.");
	                }
	                IAutenticatedApplication autenticatedApplication = (IAutenticatedApplication) application; 
					if (autenticatedApplication.login(String.valueOf(mLogin.getValue()), String.valueOf(mPassword.getValue()))) {
						autenticatedApplication.initProtected();
						mLabel.setCaption("LOGIN_OK.LBL");
					} else {
						mLabel.setCaption("LOGIN_ERROR.LBL");
					}
			}
		});
		mWasInitCalled.set(true);
		return this;
	}
}




